package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.MallMemberRechargeFlow;
import com.rockcent.mall.lib.domain.enumclass.AuditStatus;
import com.rockcent.mall.lib.dto.MallMemberRechargeFlowDto;
import com.rockcent.mall.lib.dto.MerchantCertifyDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;

/**
 * Created by DK on 17/3/9.
 */
@Repository
public class MallMemberRechargeFlowRepositoryImpl implements MallMemberRechargeFlowRepositoryCustom {

    private static final Logger log = LoggerFactory.getLogger(MallMemberRechargeFlowRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public PageResult<MallMemberRechargeFlowDto> findMallMemberRechargePage(MallMemberRechargeFlowDto dto, PageResult pageResult) {

        SQLPageDoing doing = (sql, fields, params, typeReferenceMap) -> {
            // a.status,a.name,a.mobile ,b.last_updated ,b.type
            sql.append("select %s from mall_member_recharge_flow mm ,mall m where mm.mall_id = m.id ");


            Long mallId = dto.getMallId();

            String rechargeAccount = dto.getRechargeAccount();

            String rechargeDate = dto.getRechargeDate();

            BigDecimal rechargeAmount = dto.getRechargeAmount();

            String auditStatus = dto.getAuditStatus();

            String mallName = dto.getMallName();


            String seqNo = dto.getSeqNo();

            /**
             * 提交开始时间
             */
            String beginDate = dto.getBeginDate();

            /**
             * 提交结束时间
             */
            String endDate = dto.getEndDate();


            if (StringUtils.isNotBlank(mallName)) {
                sql.append(" AND m.name like:mallName");
                params.put("mallName", "%" + mallName + "%");
            }

            if (null != mallId) {
                sql.append(" AND mm.mall_id =:mallId and m.id =:mallId");
                params.put("mallId", mallId);
            }
            if (StringUtils.isNotBlank(rechargeAccount)) {
                sql.append(" AND mm.recharge_account =:rechargeAccount");
                params.put("rechargeAccount", rechargeAccount);
            }
            if (null != rechargeAmount) {
                sql.append(" AND mm.recharge_amount =:rechargeAmount");
                params.put("rechargeAmount", rechargeAmount);
            }
            if (StringUtils.isNotBlank(seqNo)) {
                sql.append(" AND mm.seq_no =:seqNo");
                params.put("seqNo", seqNo);
            }
            if (StringUtils.isNotBlank(rechargeDate)) {
                sql.append(" AND mm.recharge_date =:rechargeDate");
                params.put("rechargeDate", rechargeDate);
            }


            if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
                sql.append(" AND mm.date_created >=:commitStartDate AND mm.date_created <:commitEndDate");
                params.put("commitStartDate", RDateUtils.beforeDateStr(beginDate, 0));
                params.put("commitEndDate", RDateUtils.afterDateStr(endDate, 1));

            } else if (StringUtils.isNotBlank(beginDate)) {
                sql.append(" AND mm.date_created >=:commitStartDate");
                params.put("commitStartDate", RDateUtils.beforeDateStr(beginDate, 0));
            } else if (StringUtils.isNotBlank(endDate)) {
                sql.append(" AND mm.date_created <:commitEndDate");
                params.put("commitEndDate", RDateUtils.afterDateStr(endDate, 1));
            }

            if (StringUtils.isNotBlank(auditStatus)) {
                if ("AUDITED".equals(auditStatus)) {
                    sql.append(" AND mm.audit_status in ('PASS','REFUSE')");
                }
                else if ("ALL".equals(auditStatus)) {
                    sql.append(" AND mm.audit_status in ('PASS','REFUSE','WAIT')");
                }else{
                    sql.append(" AND mm.audit_status =:auditStatus");
                    params.put("auditStatus", auditStatus);
                }
            }
            if ("AUDITED".equals(auditStatus)) {
                sql.append(" order by mm.date_audited desc");
            }else{
                sql.append(" order by mm.date_created desc");
            }
            fields.append(" mm.*, m.name mallName ");

            log.debug(sql.toString());

        };
        return doing.go(pageResult, MallMemberRechargeFlowDto.class, objectMapper, jdbcTemplate, log);

    }
}
